package com.example.floatingwindow

import android.annotation.SuppressLint
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.view.*
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast


class FloatingWindowService : Service(){

    private lateinit var layoutParams: WindowManager.LayoutParams
    private lateinit var windowManager: WindowManager

    override fun onBind(intent: Intent): IBinder? {
        // TODO: Return the communication channel to the service.
        throw UnsupportedOperationException("Not yet implemented")
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        showFloatingWindow()
        return super.onStartCommand(intent, flags, startId)
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun showFloatingWindow() {
        // 获取WindowManager服务
        windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager

        // 新建悬浮窗控件
        val view = LayoutInflater.from(this).inflate(R.layout.window, null)

        // 设置LayoutParam
        layoutParams = WindowManager.LayoutParams()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE
        }

        layoutParams.gravity = Gravity.LEFT or Gravity.TOP

        layoutParams.x = windowManager.defaultDisplay.width
        layoutParams.y = 200

        layoutParams.flags =
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR or
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH

        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT

        // 将悬浮窗控件添加到WindowManager


        view.setOnTouchListener(FloatingOnTouchListener())

        val close = view.findViewById<ImageView>(R.id.close)

        close.setOnClickListener {
            stopSelf()
            windowManager.removeView(view)
            Toast.makeText(this,"close",Toast.LENGTH_SHORT).show()
        }

        windowManager.addView(view, layoutParams)
    }

    override fun onDestroy() {
        super.onDestroy()
        Toast.makeText(this,"onDestroy",Toast.LENGTH_SHORT).show()
    }

    inner class FloatingOnTouchListener : View.OnTouchListener {

        private var x = 0f
        private var y = 0f

        @SuppressLint("ClickableViewAccessibility")
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            when(event?.action){

                MotionEvent.ACTION_DOWN ->{
                    x = event.rawX
                    y = event.rawY
                }

                MotionEvent.ACTION_MOVE ->{
                    val nowX = event.rawX
                    val nowY = event.rawY
                    val movedX = nowX - x
                    val movedY = nowY - y
                    x = nowX
                    y = nowY
                    layoutParams.x = (layoutParams.x + movedX).toInt()
                    layoutParams.y = (layoutParams.y + movedY).toInt()
                    windowManager.updateViewLayout(v, layoutParams)
                }

                MotionEvent.ACTION_UP ->{

                }

            }
            return false
        }
    }
}

